package com.binvshe.binvshe.app.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.MyResponseParser;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.organization.OpusAdapter;
import com.binvshe.binvshe.organization.entity.OpusListEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;

import java.util.List;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/19
 * 最新作品fragment
 */
public class NewOpusFragment extends SwipeListFragment {

    private final static int PAGE_SIZE = 10;
    private int lastListSize = 0;

    MyResponseParser<OpusListEntity> parser = new MyResponseParser<OpusListEntity>(OpusListEntity.class) {

        @Override
        public boolean hasMore() {
            if (lastListSize >= PAGE_SIZE) {
                return true;
            } else {
                return false;
            }
        }
    };

    ListDataRequestListener mRequestListener = new ListDataRequestListener(this, parser) {
        @Override
        public void onSuccess(Object listData) {
            super.onSuccess(listData);
            if (getData().size() > 0) {
                mlastid = ((OpusListEntity) getData().get(getData().size() - 1)).getId();
            }
            List<OpusListEntity> list = (List<OpusListEntity>) listData;
            if (list != null) {
                lastListSize = list.size();
            }
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        return view;
    }

    int mlastid = 0;

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        mlastid = 0;
        getData().clear();
        startLoading();
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new OpusAdapter(getActivity());
    }

    @Override
    public void onStartLoading() {
        getOpusList();
    }

    /**
     * 获取列表数据
     */
    public void getOpusList() {
        IHttpRequest request = new MyHttpRequest(getContext());
        UrlParams params = new UrlParams();
        params.put("size", PAGE_SIZE + "");
        params.put("last_id", mlastid + "");
        request.get(UrlCenter.GET_NEW_OPUS_LIS, params, mRequestListener);

    }
}
